package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import mappers.MemberMapper;
import domains.Member;

public class AddMemberGUI {
	private JFrame addMemberFrame;
	private JPanel btnsPanel, panel;
	private JButton addBtn, cancelBtn;
	private JLabel fNameLbl, lNameLbl, addressLbl, zipCodeLbl, cityLbl, emailLbl,
					phoneNoLbl;
	private JTextField fNameTF, lNameTF, addressTF, zipCodeTF, cityTF, emailTF,
	phoneNoTF; ;
	private GridLayout layout;
	
	public AddMemberGUI(){
			
		layout = new GridLayout(0,2,1,0);
		
		panel = new JPanel();
		panel.setLayout(layout);
		layout.setVgap(0);
		
		fNameLbl = new JLabel("Fornavn: ");
		fNameTF = new JTextField(15);
		
		lNameLbl = new JLabel("Efternavn: ");
		lNameTF = new JTextField(15);
		
		addressLbl = new JLabel("Adresse: ");
		addressTF = new JTextField(15);
		

		zipCodeLbl = new JLabel("Postnummer: ");
		zipCodeTF = new JTextField(15);
		
		cityLbl = new JLabel("By: ");
		cityTF = new JTextField(15);
		
		emailLbl = new JLabel("Email: ");
		emailTF = new JTextField(15);
		
		phoneNoLbl = new JLabel("Telefonnr: ");
		phoneNoTF = new JTextField(15);
			
		panel.add(fNameLbl);
		panel.add(fNameTF);
		panel.add(lNameLbl);
		panel.add(lNameTF);
		panel.add(addressLbl);
		panel.add(addressTF);
		panel.add(zipCodeLbl);
		panel.add(zipCodeTF);
		panel.add(cityLbl);
		panel.add(cityTF);
		panel.add(emailLbl);
		panel.add(emailTF);
		panel.add(phoneNoLbl);
		panel.add(phoneNoTF);
		
		
		//------------
		
		btnsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		addBtn = new JButton("Tilføj");
		addBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int zipCode = Integer.parseInt(zipCodeTF.getText());
				Member m = new Member(fNameTF.getText(), lNameTF.getText(), 
						addressTF.getText(), zipCode, cityTF.getText(),
						emailTF.getText(), phoneNoTF.getText());
				MemberMapper mMapper = new MemberMapper();
				mMapper.addMember(m);
				
				int dialog = JOptionPane.showConfirmDialog(null, "Vil du gerne se oversigten over medlemmer?", 
						"WARNING", JOptionPane.YES_NO_OPTION);
				if(dialog == JOptionPane.YES_OPTION) {
					ShowMembersGUI showMembersGUI = new ShowMembersGUI();
					showMembersGUI.showGUI();
					addMemberFrame.setVisible(false);
				} else {
					addMemberFrame.setVisible(false);
				}
			}	
		});
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addMemberFrame.setVisible(false);
			}	
		});
		
		btnsPanel.add(addBtn);
		btnsPanel.add(cancelBtn);
		
		
		
	}
	
	public void showGUI() {
		addMemberFrame = new JFrame();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
		int w = addMemberFrame.getSize().width;
		int h = addMemberFrame.getSize().height;
		int x = (dim.width-w)/4;
		int y = (dim.height-h)/4;
		addMemberFrame.setLocation(x, y);
		
		addMemberFrame.setTitle("Tilføj Medlem");
		addMemberFrame.setLayout(new BorderLayout());
		addMemberFrame.setSize(400,300);	
		addMemberFrame.add(panel, BorderLayout.CENTER);
		addMemberFrame.add(btnsPanel, BorderLayout.SOUTH);
		//addMemberFrame.pack();
		addMemberFrame.setVisible(true);
	}
}
